﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using AppConfig;

namespace ApplicationConfiguration
{
    public class AppConfiguration
    {
        private static AppConfiguration instance;

        public static AppConfiguration getInstance()
        {
            if (instance == null)
                instance = new AppConfiguration();
            return instance;
        }

        private config appConfiguration;

        private AppConfiguration()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(config));
            try
            {
                FileStream configurationStream = new FileStream("config.xml", FileMode.Open);
                appConfiguration = (config)serializer.Deserialize(configurationStream);
                configurationStream.Close();
            }
            catch (IOException e)
            {
                throw new InvalidOperationException(Strings.ConfigFileNotFound, e);
            }
        }

        public string getProperty(string sectionName, string propertyName)
        {
            section section = appConfiguration.section.SingleOrDefault(s => s.name.Name.Equals(sectionName));
            if (section == null)
            {
                throw new InvalidOperationException(Strings.SectionNotFound.Replace("$section_name$", sectionName));
            }
            property property = section.property.SingleOrDefault(p => p.name.Name.Equals(propertyName));
            if (property == null)
            {
                throw new InvalidOperationException(Strings.PropertyNotFound.Replace("$section_name$", sectionName).Replace("$property_name$", propertyName));
            }
            return property.value;
        }

        public IDictionary<string, string> getProperties(string sectionName)
        {
            section section = appConfiguration.section.SingleOrDefault(s => s.name.Name.Equals(sectionName));
            if (section == null)
            {
                throw new InvalidOperationException(Strings.SectionNotFound.Replace("$section_name$", sectionName));
            }
            IDictionary<string, string> properties = new Dictionary<string, string>();
            foreach (property property in section.property)
                properties.Add(property.name.Name, property.value);
            return properties;
        }

        public void setProperty(string sectionName, string propertyName, string value)
        {
            section section = appConfiguration.section.SingleOrDefault(s => s.name.Name.Equals(sectionName));
            if (section == null)
            {
                throw new InvalidOperationException(Strings.SectionNotFound.Replace("$section_name$", sectionName));
            }
            property property = section.property.SingleOrDefault(p => p.name.Name.Equals(propertyName));
            if (property == null)
            {
                throw new InvalidOperationException(Strings.PropertyNotFound.Replace("$section_name$", sectionName).Replace("$property_name$", propertyName));
            }
            property.value = value;
        }
    }
}
